<template>
  <d2-container>
    <template slot="header">
      <el-form
        :inline="true"
        :model="queryForm"
        ref="queryForm"
        size="mini"
        style="margin-bottom: -18px;"
      >
        <el-form-item label="状态" prop="type">
          <el-select v-model="queryForm.type" placeholder="状态选择" style="width: 100px;">
            <el-option label="全部" value="1" />
            <el-option label="操作" value="2" />
            <el-option label="操作员" value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label prop="searchVal">
          <el-input v-model="queryForm.searchVal" placeholder="请输入" style="width: 200px;" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchData">
            <d2-icon name="search" />查询
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template>
      <d2-crud
        ref="d2Crud"
        :columns="columns"
        :data="data"
        :selectionRow="true"
        :rowHandle="rowHandle"
        @row-remove="handleRowRemove"
        edit-title="查看"
        :edit-template="addTemplate"
        :form-options="formOptions"
              @row-edit="handleRowEdit"
        @dialog-open="handleDialogOpen"
        @dialog-cancel="handleDialogCancel"
        :loading="loading"
        :pagination="pagination"
        @pagination-current-change="paginationCurrentChange"
        selection-row
        @selection-change="handleSelectionChange"
      >
        <el-button size="mini" slot="header" style="margin-bottom: 5px" @click="emptyRow">清空</el-button>
        <el-button size="mini" slot="header" style="margin-bottom: 5px" @click="deleteRow">删除</el-button>

        <!-- <el-button slot="header" style="margin-bottom: 5px" @click="addRowWithNewTemplate">使用自定义模板新增</el-button> -->
      </d2-crud>
    </template>
  </d2-container>
</template>

<script>
export default {
  name: "systemLog",
  data() {
    return {
      loading:false,
      queryForm: {
        type: "1",
        user: "",
        key: "",
        note: ""
      },
      rowHandle: {
        columnHeader: "操作",
        width: 150,
        edit: {
          text: "查看",
          size: "mini",
          fixed: "right"
        },
        remove: {
          size: "mini",
          fixed: "left",
          confirm: true
        }
      },
      columns: [
        {
          title: "序号",
          key: "serialNum",
          width: 50
        },
        {
          title: "操作",
          key: "operation",
          sortable: true
        },
        {
          title: "操作员",
          key: "operator",
          sortable: true
        },
        {
          title: "IP",
          key: "ip",
          sortable: true
        },
        {
          title: "内容",
          key: "content",
          sortable: true
        },
        {
          title: "创建日期",
          key: "createDate",
          sortable: true
        }
      ],
      data: [],
      pagination: {
        currentPage: 1,
        pageSize: 100,
        total: 0
      },
      addTemplate: {
        operation: {
          title: "操作",
          value: "",
          component:{
            disabled:true
          }
        },
        operator: {
          title: "操作员",
          value: "",
          component:{
            disabled:true
          }
        },
        content: {
          title: "内容",
          value: "",
          component:{
            disabled:true
          }
        },
        parameter: {
          title: "请求参数",
          value: "",
          component:{
            disabled:true
          }
        },
        ip: {
          title: "IP",
          value: "",
          component:{
            disabled:true
          }
        },
        createDate: {
          title: "创建时间",
          value: "",
          component:{
            disabled:true
          }
        }
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      }
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    //删除请求
    deleteRowAction(ids) {
      this.loading = true;
      this.$api
        .admin_log_delete(ids)
        .then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.fetchData();
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //删除操作
    deleteRow() {
      this.$confirm("确定删除吗?", "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteRowAction(this.selectionRow.map(item => item.id));
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    emptyRowAction() {
      this.loading = true;
      this.$api
        .admin_log_clearAll()
        .then(res => {
          this.$message({
            message: "操作成功",
            type: "success"
          });
          this.fetchData();
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //清空操作
    emptyRow() {
      this.$confirm("确定清空吗？", "清空", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.emptyRowAction();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    //勾选
    handleSelectionChange(selection) {
      this.selectionRow = selection;
    },
    //分页操作
    paginationCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.fetchData();
    },
    fetchData() {
      this.loading = true;
      this.$api
        .admin_log_getPageList({
          pageNum: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          operation: this.queryForm.type == 2 ? this.queryForm.searchVal : "",
          operator: this.queryForm.type == 3 ? this.queryForm.searchVal : "",
          orderColumn: "createDate",
          orderType:'desc'
        })
        .then(res => {
          this.data = res.data;
          this.pagination.total = res.total;
          this.loading = false;
        })
        .catch(err => {
          console.log("err", err);
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleRowRemove({ index, row }, done) {
      this.deleteRowAction([row.id]);
      done();
    },

    handleDialogOpen({ mode, row, index }) {
      if (mode === "edit") {
        this.$api.admin_log_get({ id: row.id }).then(res => {
          this.$refs.d2Crud.setFormData(res);
        });
      }
    },

    handleRowEdit(row, done) {
      this.$message({
        message: "关闭弹框",
        type: "warning"
      });
      done();
    },
    handleDialogCancel(done) {
      this.$message({
        message: "关闭弹框",
        type: "warning"
      });
      done();
    }
  }
};
</script>
